//
//  ChainSearchView.m
//  Chain
//
//  Created by zhaosh on 2018/4/3.
//  Copyright © 2018年 zhaosh. All rights reserved.
//
#import "ChainSearchView.h"

@interface ChainSearchView ()<UITextFieldDelegate>
@property (nonatomic,strong)UIView *bView;
@property (nonatomic,strong)UIButton *leftBtn;
@property (nonatomic,strong)UIButton *rightBtn;
@property (nonatomic,strong)UIButton *backgraoundBtn;
@end

@implementation ChainSearchView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setContentView];
    }
    return self;
}

- (void)setContentView
{
    self.bView = [[UIView alloc]init];
    [self addSubview:self.bView];
    
    self.searchTextF = [[UITextField alloc]init];
    self.searchTextF.delegate = self;
    self.searchTextF.textAlignment = NSTextAlignmentLeft;
    self.searchTextF.autocapitalizationType = UITextAutocapitalizationTypeNone;//关闭大写
    self.searchTextF.autocorrectionType = UITextAutocorrectionTypeNo;//关闭纠正
    self.searchTextF.returnKeyType = UIReturnKeySearch;
    self.searchTextF.secureTextEntry = NO;
    self.searchTextF.borderStyle=UITextBorderStyleRoundedRect;
 
    self.searchTextF.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.searchTextF.font = FONT_LB(14.0);
    self.searchTextF.backgroundColor=[UIColor whiteColor];
 
    self.searchTextF.textColor = [UIColor colorWithHexString:@"#999999"];
    self.searchTextF.layer.cornerRadius = 14;
    self.searchTextF.layer.masksToBounds = YES;
    self.searchTextF.layer.borderWidth = 0.5;
    self.searchTextF.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.bView addSubview:self.searchTextF];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textfieldDidChanged:) name:UITextFieldTextDidChangeNotification object:self.searchTextF];
    [self.searchTextF addTarget:self action:@selector(textDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    self.leftBtn = [[UIButton alloc]init];
    [self.leftBtn setImage:[UIImage imageNamed:@"search_leftImg"] forState:(UIControlStateNormal)];
    //[self.leftBtn addTarget:self action:@selector(searchBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.leftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -5);
    self.searchTextF.leftView = self.leftBtn;
    self.searchTextF.leftViewMode = UITextFieldViewModeAlways;
    
    self.rightBtn = [[UIButton alloc]init];
    [self.rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.rightBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 4);
    self.searchTextF.rightView = self.rightBtn;
    
    self.searchTextF.rightViewMode = UITextFieldViewModeNever;
    
    self.backgraoundBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.backgraoundBtn addTarget:self action:@selector(backgroundBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.backgraoundBtn.hidden = YES;
    [self.bView addSubview:self.backgraoundBtn];
    
    [self.searchTextF addObserver:self forKeyPath:@"textOb" options:NSKeyValueObservingOptionNew  context:@"textObserver"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"text"])
    {
        if (self.searchTextF.text.length == 0) {
            self.searchTextF.rightViewMode = UITextFieldViewModeNever;
            if ([self.delegate respondsToSelector:@selector(FXScheduleSearchVCleared)]) {
                [self.delegate FXScheduleSearchVCleared];
            }
        }else{
            self.searchTextF.rightViewMode = UITextFieldViewModeAlways;
        }
    }
}

- (void)setSearchViewType:(SearchViewType)searchViewType{
    _searchViewType = searchViewType;
    CGFloat space = 5;
    space =  !self.haveSpace ? 5 : 0;
    switch (searchViewType) {
        case FXSearchViewTypeHeadV:
            self.backgroundColor = HEXCOLOR(@"#efefef");
            [self.rightBtn setImage:[UIImage imageNamed:@"deleteWhite"] forState:(UIControlStateNormal)];
            self.bView.frame = CGRectMake(0, space, self.width, self.height - 10);
            self.backgraoundBtn.frame = CGRectMake(0, 0, self.bView.width, self.bView.height);
            self.searchTextF.frame = CGRectMake(10,8, self.width-20,self.height-10-16);
            self.leftBtn.frame = CGRectMake(0, 0, 40, self.searchTextF.height);
            self.rightBtn.frame = CGRectMake(0, 0, 40,self.searchTextF.height);
            self.bView.backgroundColor = [UIColor whiteColor];
            self.leftBtn.backgroundColor = [UIColor whiteColor];
            self.rightBtn.backgroundColor = [UIColor whiteColor];
            self.searchTextF.textColor = LableTextColor666666;
            self.searchTextF.layer.borderColor = HEXCOLOR(@"#dadada").CGColor;
            break;
        case FXSearchViewTypeTitleV:
            self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
            [self.rightBtn setImage:[UIImage imageNamed:@"deleteBlack"] forState:(UIControlStateNormal)];
            self.bView.frame = CGRectMake(0, 0, self.width, self.height);
            self.backgraoundBtn.frame = CGRectMake(0, 0, self.bView.width, self.bView.height);
            self.searchTextF.frame = CGRectMake(0,8, self.width-10,self.height-16);
            self.leftBtn.frame = CGRectMake(0, 0, 40, self.searchTextF.height);
            self.rightBtn.frame = CGRectMake(0, 0, 40,self.searchTextF.height);
            
            [self.searchTextF setBackgroundColor:[UIColor whiteColor]];
            self.bView.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0];
            self.leftBtn.backgroundColor = [UIColor clearColor];
            self.rightBtn.backgroundColor = [UIColor clearColor];
            self.searchTextF.font = FONT_LB(14.0);
            self.searchTextF.textColor = HEXCOLOR(@"999999");
            self.searchTextF.layer.borderColor = HEXCOLOR(@"#ffffff").CGColor;
            
            break;
        default:
            break;
    }
}
- (void)setTextFCanEdit:(BOOL)textFCanEdit{
    _textFCanEdit = textFCanEdit;
    self.backgraoundBtn.hidden = textFCanEdit;
}

- (void)setPlaceholder:(NSString *)placeholder{
    _placeholder = placeholder;
    if ([placeholder isBlankString]){
        return;
    }
    NSMutableAttributedString *placeholder1 = [[NSMutableAttributedString alloc] initWithString:placeholder];
    [placeholder1 addAttribute:NSForegroundColorAttributeName
                         value:LableTextColor999999
                         range:NSMakeRange(0, placeholder.length)];
    [placeholder1 addAttribute:NSFontAttributeName
                         value:[UIFont systemFontOfSize:14.0]
                         range:NSMakeRange(0, placeholder.length)];
    self.searchTextF.attributedPlaceholder = placeholder1;
    
}

- (void)rightBtnAction{
    [self.searchTextF endEditing:YES];
    if (self.delegate && [self.delegate respondsToSelector:@selector(FXScheduleSearchVDelegateRightBtnAction:)]) {
        [self.delegate FXScheduleSearchVDelegateRightBtnAction:self.searchTextF];
        if (self.searchTextF.text.length == 0) {
            self.searchTextF.rightViewMode = UITextFieldViewModeNever;
        }
    }else{
        self.searchTextF.rightViewMode = UITextFieldViewModeNever;
    }
}

-(BOOL)textFieldShouldReturn:(UITextField* )textField
{
    [self getTextFiledText:textField.text];
    return YES;
}

- (void)getTextFiledText:(NSString *)text{
    [self.searchTextF endEditing:YES];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(FXScheduleSearchVDelegateTextFieldShouldReturn:)]) {
        [self.delegate FXScheduleSearchVDelegateTextFieldShouldReturn:text];
    }
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [self setSearchTextFRightViewMode:textField.text];
    if (self.startEditingBlock) {
        self.startEditingBlock(YES);
    }
    return YES;
}


-(void)textfieldDidChanged:(NSNotification *)notfi{
    UITextField *tf = (UITextField *)notfi.object;
    if (tf.text.length == 0) {
        self.searchTextF.rightViewMode = UITextFieldViewModeNever;
        if ([self.delegate respondsToSelector:@selector(FXScheduleSearchVCleared)]) {
            [self.delegate FXScheduleSearchVCleared];
        }
        if (self.textLengthToZeroBlock) {
            self.textLengthToZeroBlock();
        }
    }else{
        self.searchTextF.rightViewMode = UITextFieldViewModeAlways;
    }
}

- (void)backgroundBtnAction{
    if (self.delegate && [self.delegate respondsToSelector:@selector(FXScheduleSearchVDelegateTextFNotEdit:)]) {
        [self.delegate FXScheduleSearchVDelegateTextFNotEdit:YES];
    }
    if (self.titleClickedBlock) {
        self.titleClickedBlock();
    }
}

- (void)setSearchTextFRightViewMode:(NSString *)text{
    if (text.isBlankString) {
        self.searchTextF.rightViewMode = UITextFieldViewModeNever;
    } else {
        self.searchTextF.rightViewMode = UITextFieldViewModeAlways;
    }
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextFieldTextDidChangeNotification object:self.searchTextF];
    
    [self.searchTextF removeObserver:self forKeyPath:@"textOb"];
}

#pragma mark - 限制textField输入字数
-(void)textDidChange:(UITextField *)textField{
    NSInteger textNum=self.canInputTextNum?self.canInputTextNum:100;
    [textField limitTextFieldWithLength:textNum];
    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger textNum=self.canInputTextNum?self.canInputTextNum:100;
    return [textField isEnabledWithLength:textNum shouldChangeCharactersInRange:range replacementString:string];
}
#pragma mark -
@end

